﻿<%@ Page Language="C#" AutoEventWireup="false" CodeFile="recRestructurNextform.aspx.cs" Inherits="SIPO.Web.Forms.Transaction.recRestructurNextform" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Jadwal Pembayaran</title>
    <link href="<%= ResolveUrl("~/styles/chosen/chosen.min.css") %>" rel="stylesheet" type="text/css" />

    <script src="<%= ResolveUrl("~/scripts/plupload/moxie.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/plupload/plupload.full.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.min.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.validate.wrapper.js") %>" type="text/javascript"></script>
    <script src="<%= ResolveUrl("~/scripts/jquery.inputmask.bundle.min.js") %>" type="text/javascript"></script>

    <script src="<%= ResolveUrl("~/scripts/chosen/chosen.jquery.min.js") %>" type="text/javascript"></script>

    <style type="text/css">
        .style1
        {
            width: 225px;
        }
        .style2
        {
            width: 111px;
        }
    </style>

    <script type="text/javascript">
    var edit = '<%= Request.Params["edit"] %>';
        var IsView = false;
        activateEdit = function () {
            //  alert();
            //$("#btnSave").hide();
            if (edit != 1) {
                $("#btnCreateRow").hide();
                $("#btnReset").hide();
                  }
            else {
                $("#btnCreateRow").prop('disabled', true);
                            }
            $("#rbSchemaMulti").prop('disabled', true);
            $("#rbSchemaSingle").prop('disabled', true);
            $("#txtNilai").inputmask("decimal", { radixPoint: ",", autoGroup: true, groupSeparator: ".", groupSize: 3, digits: 2, repeat: 18 });

            jQuery("#table1").jqGrid('setGridParam', { url: handlerUrl + '?method=getschedforview' }).trigger("reloadGrid");
            setTimeout("$('#table1').trigger('reloadGrid');", 500);
            setTimeout("a();", 1000);
            $("#txtRbMulti").prop('disabled', true);
            IsView = true;

        };
//         function cek() {
//             if ($("#table1").jqGrid('getGridParam', 'records') < 1) {
//                 setTimeout("$('#table1').trigger('reloadGrid');", 500);
//               
//                 setTimeout("a();", 1000);
//             }
//        }

        function a() {
          //  alert();
            if ($("#table1").jqGrid('getGridParam', 'records') > 1) {
                $("#rbSchemaMulti").prop("checked", true);
                $("#txtRbMulti").val($("#table1").jqGrid('getGridParam', 'records'));
            }
            if ( edit != 1) {
                $("#btnSave").hide();
            }
//            setTimeout("cek();", 1000);
        }
    </script>

    <%--Handle Grid--%>
    <script type="text/javascript">
        var handlerUrl = '<%= ResolveUrl("~/AjaxHandler/Transaction/recRestructurhandler.ashx") %>';

        $('#table1').jqGrid({
            datatype: 'json',
            colNames: ['Pembayaran Ke-', 'Nilai', 'Tgl. Jatuh Tempo', 'Keterangan'],
            colModel: [
                    { name: 'ScheduleTime', index: 'ScheduleTime', width: 100, align: 'center' },
                    { name: 'PaymentValue', index: 'PaymentValue', editable: true, formatter: 'currency', align: 'right',
                        formatoptions: { decimalSeparator: ",", thousandsSeparator: ".", decimalPlaces: 2 }, width: 100
                    },
                    { name: 'DueDate', index: 'DueDate', formatter: 'date', formatoptions: { newformat: 'd M Y' }, width: 100, align: 'center', editable: true, editoptions: { dataInit: function (el) {
                        $(el).datepicker({ dateFormat: 'yy-mm-dd' });
                    }
                    }
                    },
                    { name: 'ScheduleNote', index: 'ScheduleNote', width: 250, editable: true }
           	],
            rowNum: 10,
            id: 'ScheduleTime',
            rownumber: true,
            sortname: 'ScheduleTime',
            sortorder: "asc",
            viewrecords: true,
            jsonReader: { repeatitems: false },
            width: 700,
            cellEdit: true,
            cellsubmit: 'clientArray',
            height: '100%',
            caption: 'Transaksi',
            ajaxGridOptions: { cache: false },
            cellsubmit: 'clientArray',
            beforeSelectRow: function (rowid, e) {
                if (IsView == false || edit == 1) {

                    return true;
                }
                else {
                    return false;
                }
            },
            loadComplete: function () {
                if (edit != "") {
                    var sum = 0;
                    grid = jQuery("#table1");
                    var ids = grid.jqGrid('getDataIDs');
                    for (var i = 0; i < ids.length; i++) {
                        var rowId = ids[i];
                        var val = parseInt(grid.jqGrid('getCell', rowId, 'PaymentValue').replace("Rp.", ""));
                        sum = parseInt(sum) + val;
                    }
                    var nilai = parseInt($("#txtNilai").val().replace(".", "").replace(".", "").replace(".", "").replace(".", "").replace(".", ""));
                    if (sum != nilai) {

                        grid.jqGrid('clearGridData');
                        $("#rbSchemaMulti").prop('disabled', false);
                        $("#rbSchemaSingle").prop('disabled', false);
                        $("#rbSchemaSingle").prop('checked', true);
                        $("#btnCreateRow").removeAttr('disabled');
                        alert("Skema pembayaran akan diatur ulang !");
                    }
                }
            }
            //            onSelectRow: function (id) {
            //                if (id && id !== lastsel2) {
            //                    $('#table1').jqGrid('restoreRow', lastsel2);
            //                    $('#table1').jqGrid('editRow', id, true);
            //                    lastsel2 = id;
            //                }
            //            }
        });
        
        $("#table1").jqGrid('navGrid', '#pager1', { edit: true, add: false, search: false, del: false, refresh: false },
        {}, {}, {}, { closeOnEscape: true, multipleSearch: false, closeAfterSearch: true });

        $("#btnCreateRow").click(function () {
            var row = $("#txtRbMulti").val();
            var val = $("#txtNilai").val();
            if ($("#rbSchemaMulti").is(":checked")) {
                if (Number(row) > 10) {
                    alert("Maksimal schema yang bisa diinput adalah 10 !");
                } else if ((row == "") || (Number(row) == 0)) {
                    alert("Harap masukkan jumlah yang mau diinput !");
                } else {
                    $("#table1").jqGrid('setGridParam', { url: handlerUrl + '?method=getdummygrid&rowcount=' + row + '&val='+val}).trigger('reloadGrid');
                    $("#rbSchemaMulti").prop('disabled', true);
                    $("#rbSchemaSingle").prop('disabled', true);
                    $("#btnCreateRow").prop('disabled', true);
                }
            } else if ($("#rbSchemaSingle").is(":checked")) {
                $("#table1").jqGrid('setGridParam', { url: handlerUrl + '?method=getdummygrid&val=' + val }).trigger('reloadGrid');
                $("#rbSchemaMulti").prop('disabled', true);
                $("#rbSchemaSingle").prop('disabled', true);
                $("#btnCreateRow").prop('disabled', true);
            } else {
                alert("Harap pilih Skema Pembayaran !");
            }
        });
        $("#txtNilai").inputmask("decimal", { radixPoint: ",", autoGroup: true, groupSeparator: ".", groupSize: 3, digits: 2, repeat: 18 });


        $("#btnReset").click(function () {
            $("#rbSchemaMulti").prop('disabled', false);
            $("#rbSchemaSingle").prop('disabled', false);
            // $("#rbSchemaSingle").prop('checked', true);
            $("#btnCreateRow").removeAttr('disabled');
        });

        $("#btnSave").click(function () {
            if ($("#table1").jqGrid('getGridParam', 'records') < 1) {
                alert("Pilih skema pembayaran terlebih dahulu !");
                return false;
            }


            var nilai = parseInt($("#txtNilai").val().replace(".", "").replace(".", "").replace(".", "").replace(".", "").replace(".", "").replace(".", ""));

            var ids = $("#table1").jqGrid('getDataIDs');
            var sum = 0;
            for (var i = 0; i < ids.length; i++) {
                var rowId = ids[i];
                var due = $("#table1").jqGrid('getCell', rowId, 'DueDate');
                var sched = $("#table1").jqGrid('getCell', rowId, 'ScheduleNote');
                var val = parseInt($("#table1").jqGrid('getCell', rowId, 'PaymentValue').replace("Rp.", ""));
                sum = parseInt(sum) + val;
              
                    if (due.toString().toLowerCase().indexOf(2) >= 0 && due.toString() != '') {
                        //s 
                        if (sched.toString().toLowerCase().indexOf('<input') < 0 && sched.toString() != '') {
                            //alert(sched);
                        }
                        else {
                            alert('Keterangan harus di isi !');
                            return false;
                            break;

                        }

                    }
                    else {
                        //  alert(due.toString().toLowerCase().indexOf(2));
                        alert('Tanggal harus di isi !');
                        return false;
                        break;

                    }
                
            }
             if (sum != nilai) {
                   
                    alert('Skema tidak valid !');
                    return false;
                   // break;
              }


            $('#table1').setGridParam({ cellEdit: false });

            var rowData = $("#table1").jqGrid('getRowData');
            var jsonData = JSON.stringify(rowData);
            if (edit != 1) {
                handlerUrl = handlerUrl + "?method=insert&sched=" + encodeURIComponent($('input[type=radio][name=rbSchema]:checked').val())
                    + "&multi=" + encodeURIComponent($("#txtRbMulti").val());
            }
            else {
                handlerUrl = handlerUrl + "?method=update&sched=" + encodeURIComponent($('input[type=radio][name=rbSchema]:checked').val())
                    + "&multi=" + encodeURIComponent($("#txtRbMulti").val());
            }

            $.ajax({
                type: 'POST',
                url: handlerUrl,
                contentType: 'application/json',
                dataType: 'json',
                data: jsonData,
                success: function (data) {
                    if (data.IsSuccess) {
                        parent.$('#dialog').dialog('close');
                        setTimeout("refresh();", 500);
                        alert('Data berhasil disimpan !');
                    }
                    else {
                        alert('Error Message: ' + data.Message);
                    }
                },
                error: function (response) {
                    var r = JSON.parse(response.responseText);
                    alert("Error Message: " + r.Message);
                }
            });
        });

        $(document).ready(function () {
            
        });

        function refresh() {
            window.location = '<%=ResolveUrl("~/lists/Transaction/recRestructurlist.aspx") %>';           
        }
        
        
    </script>
    
    <script type="text/javascript" language="javascript">
        $(function () {
            $("#txtRbMulti").inputmask("decimal", { radixPoint: ",", autoGroup: true, groupSeparator: ".", groupSize: 3, digits: 2, repeat: 18 });

            $("#btnSave").button({ icons: { primary: "ui-icon-disk"} });
            $("#btnClose").button({ icons: { primary: "ui-icon-close"} });

            $('#btnClose').click(function () {
                parent.$('#dialog').dialog("close");

                return false;
            });

            $("#rbSchemaSingle").click(function () {
                if ($("#rbSchemaSingle").is(":checked")) {
                    $("#txtRbMulti").val();
                    $("#txtRbMulti").prop('disabled', true);
                    $("#txtRbMulti").val('');
                }
            });
            $("#rbSchemaMulti").click(function () {
                if ($("#rbSchemaMulti").is(":checked")) {
                    $("#txtRbMulti").prop('disabled', false);
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <table border="0">
        <tr>
            <td style="width: 50">
                Nama Debitur
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtNamaDebitur" runat="server" Width="300" ReadOnly="true"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                No. Surat
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtNoSK" runat="server" Width="200" ReadOnly="true"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Nilai
            </td>
            <td>
                :
            </td>
            <td>
                <asp:TextBox ID="txtNilai" runat="server" Width="200" MaxLength="10" ReadOnly="true"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td style="width: 50">
                Schema Pembayaran
            </td>
            <td>
                :
            </td>
            <td>
                <input type="radio" name="rbSchema" id="rbSchemaSingle" value="single" checked="checked" /> Single<br />
                <input type="radio" name="rbSchema" id="rbSchemaMulti" value="multi" /> Multi&nbsp;&nbsp;&nbsp;
                    <input type="text" id="txtRbMulti" name="txtRbMulti" maxlength="2" style="width:20px" disabled="disabled" />
                    <input type="button" id="btnCreateRow" name="btnCreateRow" value="Input Schema" />
                    <input type="button" id="btnReset" name="btnReset" value="Reset" />
            </td>
        </tr>
    </table>
    <br />
    <br />
    <div>
        <table id="table1" class="scroll" cellpadding="0" cellspacing="0">
        </table>
        <div id="pager1" class="scroll">
        </div>
    </div>
    <br />    
    <br />
    <div style="text-align: right; padding-top: 10px;">
        <button id="btnSave" type="button">
            Simpan</button>
        <button id="btnClose" type="button">
            Batal</button>
    </div>
    <asp:hiddenfield ID="hfID" runat="server"></asp:hiddenfield>
    </form>
</body>
</html>

